// Copyright (c) 2022, hzmes and contributors
// For license information, please see license.txt

frappe.ui.form.on('SRM Trial Production', {

	setup: function(frm) {
		if(frm.is_new() && !frm.doc.billno) {
			frappe.db.get_value('SRM Naming Series', 
			{'doctype_name': frm.doctype, 'date': frappe.datetime.get_today()}, 
			['name', 'current_series']
			).then((r)=>{
				// 返回后赋值get_series
				// get_series = r.message.current_series;
				// 获取当前日期后进行格式化转换:去掉-再拼接
				frm.doc.billno = 'GM';
				frm.doc.billno += frappe.datetime.get_today().split('-').join('');
				// 在r.message.current_series基础上以0自动补位:数字+''相当于是转换为字符串
				frm.doc.billno += (r.message.current_series + '').padStart(3,'0');
				frm.refresh_field('billno')
				// 获取后马上更新记录表中的当前流水号
				frappe.db.set_value('SRM Naming Series', 
									r.message.name, 
									'current_series', 
									r.message.current_series + 1
									);
				})
		}

	},

	refresh: function(frm) {
		// 调用日期计算函数
		frappe.call({
			method: "calc_date",
			doc: frm.doc,
			args:{date_from: frm.doc.date, days: 3 },
			freeze: true,
			freeze_message: __("running..."),
			callback: function(r) {
				frm.set_value('need_date', r.message)
				frm.refresh_field('need_date')
			}
		});

		if (frm.doc.docstatus == 1 ) {
			frm.add_custom_button("同步至ERP申购", () => {
				frm.reload_doc();
				if (frm.doc.pr_billno_from_erp) {
					frappe.msgprint(`已经生成ERP申请单:${frm.doc.pr_billno_from_erp}`);
				}
				else
					frappe.confirm('确认同步ERP采购申请单?',
						() => {
							// action to perform if Yes is selected
							frm.call({
								method: 'srm.srm.doctype.srm_server_config.srm_server_config.sync_to_k3cloud_pr',
								args: {org: frm.doc.org, material_number: frm.doc.material_number, supplier_number: frm.doc.supplier, note: frm.doc.sample_admit, entrynote: frm.doc.billno, qty: frm.doc.test_qty},
								freeze: true,
								callback: function(r) {
									frappe.db.set_value(frm.doctype, frm.docname, 'pr_billno_from_erp', r.message)
									.then( date => {	
													frm.refresh_field('pr_billno_from_erp');
													frm.reload_doc();
													frappe.msgprint(`同步ERP成功:${r.message}`);
													// console.log('sync_to_k3cloud_pr OK');
												})
									// ;
									// console.log(r.message);
								}
							})
						}, () => {
							// action to perform if No is selected
							console.log('No');
						})
			})
		};

	},

	date: function(frm) {
		// 调用日期计算函数
		frappe.call({
			method: "calc_date",
			doc: frm.doc,
			args:{date_from: frm.doc.date, days: 3 },
			freeze: true,
			freeze_message: __("running..."),
			callback: function(r) {
				frm.set_value('need_date', r.message)
				frm.refresh_field('need_date')
			}
		});
	},

	before_save: function(frm) {
		if(!frm.doc.billno) {
			frappe.db.get_value('SRM Naming Series', 
			{'doctype_name': frm.doctype, 'date': frappe.datetime.get_today()}, 
			['name', 'current_series']
			).then((r)=>{
				// 返回后赋值get_series
				// get_series = r.message.current_series;
				// 获取当前日期后进行格式化转换:去掉-再拼接
				frm.doc.billno = 'GM';
				frm.doc.billno += frappe.datetime.get_today().split('-').join('');
				// 在r.message.current_series基础上以0自动补位:数字+''相当于是转换为字符串
				frm.doc.billno += (r.message.current_series + '').padStart(3,'0');
				// 获取后马上更新记录表中的当前流水号
				frappe.db.set_value('SRM Naming Series', 
									r.message.name, 
									'current_series', 
									r.message.current_series + 1
									);
				})
		}

	},

});
